package
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class SuppressionButton extends Sprite
	{
		
		private var force:Force;
		private var forceField:ForceField;
		
		private var timer:Number;
		
		public function SuppressionButton( ff:ForceField )
		{
			
			assignForceField( ff );
			forceField.addChild( this );
			
			var stroke:Number = 1;
			var diameter:Number = 7;
			graphics.lineStyle( 4, 0xFFFFFF, 1);
			graphics.beginFill( 0xFF0000, 1 );
			graphics.drawCircle( 0, 0, diameter*2 );
			
			graphics.moveTo( -diameter+stroke, 0 );
			graphics.lineTo( diameter-stroke, 0);
			graphics.moveTo( 0, -diameter+stroke);
			graphics.lineTo( 0, diameter-stroke);
			rotation = 45;
			
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			addEventListener(Event.ENTER_FRAME, enterFrameHandler );
			hide();
			
			cacheAsBitmap = true;
			
		}
		
		public function assignForceField( ff:ForceField = null ):void
		{
			forceField = ff;
		}
		public function assignForce( f:Force = null ):void
		{
			force = f;
		}
		
		public function show():void
		{
			this.visible = true;
			this.alpha = 1;
			this.timer = getTimer() + 2000;
			assignForce( forceField.currentForce );
		}
		public function hide():void
		{
			this.visible = false;			
		}
		
		public function mouseDownHandler( e:Event ):void
		{
			if( force != null )
			{
				
				forceField.deleteForce( force );
				force = null;
				visible = false;
				
			}
				
		}
		public function enterFrameHandler( e:Event ):void
		{
			if( force != null ) 
			{
				visible = true;
				x = force.x + force.width/2; 
				y = force.y - force.height/2; 
				width = force.width /4;
				height = force.height /4;
				
			}else{
				
				hide();
			}
			if( timer < getTimer() )
			{
				cacheAsBitmap = true;
				fadeOut();
			}
			
			
		}
		function fadeOut():void
		{
			if( alpha > 0) alpha -= .1;
		}
	}
	
}